package site.shader85.Kingdoms.kingdom;

import java.util.HashSet;
import java.util.Set;

import com.google.common.collect.Sets;

import site.shader85.Kingdoms.domain.Domain;

@SuppressWarnings("unchecked")
public class Kingdom {

	private static HashSet<Kingdom> kingdoms = Sets.newHashSet();
	private HashSet<String> citizens = Sets.newHashSet();
	private HashSet<Domain> domains = Sets.newHashSet();
	private String owner;
	private String name;
	
	
	public static HashSet<Kingdom> getKingdoms() {
		return (HashSet<Kingdom>) kingdoms.clone();
	}

	private Kingdom(String name, String owner) {
		this.name = name;
		this.owner = owner;
	}
	
	public boolean equals(Object obj){
		if(obj instanceof Kingdom)
			if(((Kingdom) obj).getName().equals(name))
				return true;
		return false;
	}
	
	public int hashCode(){
		return name.hashCode();
	}
	
	public String getName() {
		return name;
	}
	
	public int getDomainSize(){
		return domains.size();
	}
	
	public String getOwner() {
		return owner;
	}
	
	public int getCitizensNumber(){
		return citizens.size();
	}
	
	public HashSet<String> getCitizens(){
		return (HashSet<String>)this.citizens.clone();
	}
	
	public int getArea() {
		int area = 0;
		for (Domain d : domains) {
			area += d.getXZ();
		}
		return area;
	}
	
	public int getV() {
		int v = 0;
		for (Domain d : domains) {
			v += d.getHeight() * d.getXZ();
		}
		return v;
	}

	public Set<Domain> getDomains() {
		return (Set<Domain>) domains.clone();
	}
	
	public void addCitizen(String name){
		for(Kingdom k:kingdoms){
			for(String n:k.citizens){
				if(n.equalsIgnoreCase(name))
					return;
			}
		}
		citizens.add(name);
	}
	
	public void giveDomain(Domain d) {
		d.setOwner(this.owner);
		domains.add(d);
	}

	public void removeDomain(Domain d) {
		domains.remove(d);
		d.setOwner(null);
	}

	public static Kingdom getFromName(String name){
		for(Kingdom k:kingdoms){
			if(k.getName().equals(name))
				return k;
		}
		return null;
	}
	
	public static Kingdom getFromOwner(String owner){
		for(Kingdom k:kingdoms){
			if(k.getOwner().equals(owner))
				return k;
		}
		return null;
	}
	
	public static Kingdom createKingdom(String name, String owner) {
		Kingdom k = new Kingdom(name, owner);
		kingdoms.add(k);
		return k;
	}
}
